<?php
namespace lib;

class Request{

    public $module;
    public $controller;
    public $action;
    public $route;
    public $data;
    public $post;
    public $get;

    /**
     * Request constructor.
     */
    function __construct(){
        $this->parseRoute();
    }

    /**
     * Quick instance
     * @return Request
     */
    public static function instance(){
        return new self();
    }

    /**
     * 解析路由参数
     */
    function parseRoute(){
        $this->data = $_REQUEST;
        $this->post = $_POST;
        $this->route = isset($this->data['query']) ? $this->data['query'] : '';
        $this->route = array_values(array_filter(explode('/', $this->route)));
        for($i = 0; $i < 3; $i ++){
            if(!isset($this->route[$i])){
                if($i == 1){
                    $this->route[$i] = 'Index';
                }else{
                    $this->route[$i] = 'index';
                }
            }
        }
        $this->controller = strtolower($this->route[1]);
        $this->module = strtolower($this->route[0]);
        $this->action = strtolower($this->route[2]);
        $this->parseParams($this->route);
        $this->get = $_GET;
    }

    /**
     * 创建一个请求
     * @param $url
     * @param array $data
     * @param string $method
     * @param array $header
     * @return mixed
     */
    function create($url, array $data = array(), $method = 'GET', array $header = array()){
        $ch = curl_init();
        $method = strtoupper($method);
        if($data){
            if('GET' == $method){
                $url .= '?' . http_build_query($data);
            }else{
                curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
            }
        }
        curl_setopt_array($ch, array(
            CURLOPT_URL => $url,
            CURLOPT_SSL_VERIFYPEER  => 0,
            CURLOPT_SSL_VERIFYHOST  => 2,
            CURLOPT_RETURNTRANSFER  => true,
            CURLOPT_CUSTOMREQUEST   => $method,
            CURLOPT_HTTPHEADER => $this->parseHeader($header),
        ));
        $result = curl_exec($ch);
        curl_close($ch);
        return $result;
    }

    /**
     * 解析伪静态参数
     * @param $action
     */
    protected function parseParams($action = null){
        if(count($action) > 3){    //获取URI中的参数
            $params = array_slice($action, 3);
            foreach($params as $key => $val){
                if($key % 2 == 0){
                    $keys[] = $val ? $val : '';
                }else{
                    $values[] = $val;
                }
            }
            if(isset($keys) && isset($values)){
                $_GET = array_merge($_GET, array_combine($keys, $values));
            }
        }
    }

    /**
     * 格式化请求头
     * @param $header
     * @return array
     */
    protected function parseHeader($header){
        $cache = array();
        foreach ($header as $key => $val){
            array_push($cache, "{$key}: $val");
        }
        return $cache;
    }

	/**
	 * @param $key
	 * @return null
	 */
    function get($key = ''){
    	return $key ? isset($this->get[$key]) ? $this->get[$key] : null : $this->get;
    }

	/**
	 * @param $key
	 * @return null
	 */
    function post($key = ''){
	    return $key ? isset($this->post[$key]) ? $this->post[$key] : null : $this->post;
    }

	/**
	 * @param $key
	 * @return null
	 */
    function data($key = ''){
	    return $key ? isset($this->data[$key]) ? $this->data[$key] : null : $this->data;
    }

	/**
	 * @param $key
	 * @return null
	 */
    function file($key = ''){
    	return $key ? isset($_FILES[$key]) ? $_FILES[$key] : null : $_FILES;
    }

}